﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;      
using System.Web.Mvc;
using System.Web.Mvc.Html;

namespace BOSS.Web.Extensions
{
    public static class EncodeExtensions
    {
        /// <summary>
        /// 编码枚举类型的值
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string EncodeEnum(this HtmlHelper htmlHelper, Enum value)
        {
            return EncodeEnum(htmlHelper, value, string.Empty, null);
        }

        /// <summary>
        /// 编码枚举类型的值
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="value"></param>
        /// <param name="blankAndZeroText"></param>
        /// <returns></returns>
        public static string EncodeEnum(this HtmlHelper htmlHelper, Enum value, string blankAndZeroText)
        {
            return EncodeEnum(htmlHelper, value, blankAndZeroText, blankAndZeroText);
        }

        /// <summary>
        /// 编码枚举类型的值
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="value"></param>
        /// <param name="blankText"></param>
        /// <param name="zeroText"></param>
        /// <returns></returns>
        public static string EncodeEnum(this HtmlHelper htmlHelper, Enum value, string blankText, string zeroText)
        {
            if (value == null)
            {
                return blankText;
            }

            if (zeroText != null)
            {
                int intValue = Convert.ToInt32(value);
                if (intValue == 0)
                {
                    return zeroText;
                }
            }

            return value.GetDescription();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="htmlHeler"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string EncodeDateTime(this HtmlHelper htmlHeler, DateTime? value)
        {
            if (value == null)
            {
                return string.Empty;
            }

            return htmlHeler.EncodeDateTime(value.GetValueOrDefault());
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="htmlHeler"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string EncodeDateTime(this HtmlHelper htmlHeler, DateTime value)
        {
            return htmlHeler.Encode(value.ToString("yyyy-MM-dd HH:mm:ss"));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="htmlHeler"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string EncodeDate(this HtmlHelper htmlHeler, DateTime? value)
        {
            if (value == null)
            {
                return string.Empty;
            }

            return htmlHeler.EncodeDate(value.GetValueOrDefault());
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="htmlHeler"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string EncodeDate(this HtmlHelper htmlHeler, DateTime value)
        {
            return htmlHeler.Encode(value.ToString("yyyy-MM-dd"));
        }
    }
}
